package com.example.myapplication;

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.POJO.StockStrategy;
import com.example.myapplication.utils.RabbitMqUtils;
import com.example.myapplication.utils.RedisUtils;
import com.google.gson.Gson;
import com.rabbitmq.tools.json.JSONUtil;

import org.json.JSONObject;
import org.json.JSONStringer;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    EditText codeText = null;
    EditText stockNameText = null;
    EditText buyPriceText = null;
    EditText buyAmountText = null;
    EditText stopLossText = null;
    EditText takeProfitText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        //content-main.xml button save event
        Button saveButton = (Button) findViewById(R.id.save);
        codeText = (EditText) findViewById(R.id.code);
        stockNameText = (EditText) findViewById(R.id.name);
        buyPriceText = (EditText) findViewById(R.id.buyPrice);
        buyAmountText = (EditText) findViewById(R.id.buyAmount);
        takeProfitText = (EditText) findViewById(R.id.takeProfit);
        stopLossText = (EditText) findViewById(R.id.stopLoss);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String stockCode = codeText.getText().toString();
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("保存策略");
                //builder.setMessage("code不能为空");
                builder.setPositiveButton("确认", listener);
                builder.setNegativeButton("取消", listener);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });


    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (which == -1) {
                String code = codeText.getText().toString();
                if ("".equals(code)) {

                    Toast.makeText(MainActivity.this, "股票代码不能为空", Toast.LENGTH_SHORT).show();
                    return;

                }

                String stockName = stockNameText.getText().toString();
                if ("".equals(stockName)) {
                    Toast.makeText(MainActivity.this, "股票名称不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String buyPrice = buyPriceText.getText().toString();
                if ("".equals(buyPrice)) {
                    Toast.makeText(MainActivity.this, "买入价不能为空", Toast.LENGTH_SHORT).show();
                    return;

                }
                String buyAmount = buyAmountText.getText().toString();
                if ("".equals(buyAmount)) {
                    Toast.makeText(MainActivity.this, "买入数量不能为空", Toast.LENGTH_SHORT).show();
                    return;

                }
                String takeProfit = takeProfitText.getText().toString();
                if ("".equals(takeProfit)) {
                    Toast.makeText(MainActivity.this, "止盈价不能为空", Toast.LENGTH_SHORT).show();
                    return;

                }
                String stopLoss = stopLossText.getText().toString();
                if ("".equals(stopLoss)) {
                    Toast.makeText(MainActivity.this, "止损价不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                final StockStrategy stockStrategy=new StockStrategy();
                stockStrategy.setCode(code);
                stockStrategy.setName(stockName);
                stockStrategy.setBuyAmount(buyAmount);
                stockStrategy.setBuyPrice(buyPrice);
                stockStrategy.setStatus(1);
                stockStrategy.setStopLossPrice(stopLoss);
                stockStrategy.setTakeProfitPrice(takeProfit);
                RabbitMqUtils.exec.execute(new Runnable() {
                    @Override
                    public void run() {
                        RabbitMqUtils.publishToAMQP(RabbitMqUtils.gson.toJson(stockStrategy));

                    }
                });

                //确定
                Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

            } else if (which == 2) {
                //取消
                Toast.makeText(MainActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
            }
        }
    };

}
